package com.dubito.test;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.dubito.mappers.StudentMapper2;
import com.dubito.model.Student2;
import com.dubito.utils.SqlSessionFactoryUtil;

public class StudentTest3 {

		private static Logger logger = Logger.getLogger(StudentTest3.class);
		private SqlSession session = null;
		private StudentMapper2 mapper = null;
		
		/**
		 * 先执行
		 * @throws Exception
		 */
		@Before
		public void setUp() throws Exception {
			session=SqlSessionFactoryUtil.openSession();
			
			mapper=session.getMapper(StudentMapper2.class);
		}
		
		/**
		 * 后执行
		 * @throws Exception
		 */
		@After
		public void tearDown() throws Exception {
			session.close();
		}
		
		/*
		 * 查询
		 * 
		 * */
		@Test
		public void searchStudent(){
			logger.info("查询学生，带条件");
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("gradeId", 2);
			map.put("name","%王%");
			map.put("age", 23);
			List<Student2> list = mapper.searchStudent(map);
			for (Student2 student : list) {
				System.out.println(student);
			}
			
		}
		
		@Test
		public void searchStudent2(){
			logger.info("查询学生，带条件");
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("serach", "name");
			map.put("gradeId", 2);
			map.put("name","%王%");
			map.put("age", 23);
			List<Student2> list = mapper.searchStudent2(map);
			for (Student2 student : list) {
				System.out.println(student);
			}
			
		}
		@Test
		public void searchStudent3(){
			logger.info("查询学生，带条件");
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("gradeId", 2);
			map.put("name","%王%");
			map.put("age", 23);
			List<Student2> list = mapper.searchStudent3(map);
			for (Student2 student : list) {
				System.out.println(student);
			}
			
		}
		@Test
		public void searchStudent4(){
			logger.info("查询学生，带条件");
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("gradeId", 2);
			//map.put("name","%王%");
			map.put("age", 23);
			List<Student2> list = mapper.searchStudent4(map);
			for (Student2 student : list) {
				System.out.println(student);
			}
			
		}
		@Test
		public void searchStudent5(){
			logger.info("查询学生，带条件");
			Map<String,Object> map = new HashMap<String,Object>();
			ArrayList<Integer> gradeIds = new ArrayList<Integer>();
			gradeIds.add(1);
			gradeIds.add(3);
			map.put("gradeIds", gradeIds);
		
			List<Student2> list = mapper.searchStudent5(map);
			for (Student2 student : list) {
				System.out.println(student);
			}
			
		}
		
		@Test
		public void updateStudent(){
			logger.info("更新学生，带条件");
			Student2 student = new Student2();
			student.setId(1);
			student.setAge(55);
			//student.setName("张小三");
			mapper.updateStudent(student);
			session.commit();
			System.out.println(student);
		}
}
